<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class update extends CI_Controller {
	public function __construct () {
	
		//Not allow anyone to access this controller.
		// return ;
		parent::__construct ();
		$this->load->database();
	}
	
	function indexx () {
		$this->download(
			'program', 
			'PROG_THUMB', 
			FCPATH . "dataupload\\program\\thumb\\");
	}
	
	public function image_program() {
		$programs = $this->db
			->get('program')->result();
		
		foreach ($programs as $program) {
			$name = basename($program->PROG_COVER);
			if ($name) {
				$fixedUrl = "dataupload/program/cover/".$name;
				$this->db
					->where('PROG_ID', $program->PROG_ID)
					->update('program', array(
						'PROG_COVER' =>$fixedUrl
					));
				echo "UPDATED: ". $fixedUrl;
			
				echo "<br />";
			}
		}
		
	}
	
	public function image_video() {
		$path = FCPATH . "dataupload\\video\\thumb\\";
		$url_path = "dataupload/video/thumb/";
		// return ;
		$videos = $this->db
			->get('video')->result();
		
		
		foreach ($videos as $video)  {
			$name = basename($video->VID_THUMB);
			$filename = $video->VID_CODE . "-". $video->VID_ID .".". pathinfo($video->VID_THUMB, PATHINFO_EXTENSION);;
			
			//Update to database;
			$this->db
				->where ('VID_ID', $video->VID_ID)
				->update('video', array(
					'VID_THUMB' =>$url_path . $filename, 
				));
			
			
			//Download file;
			// file_put_contents($path . $filename, file_get_contents($video->VID_THUMB));
			
			echo "UPDATED: " . $url_path . $filename;
			echo "<br />";
		}
	}
	
	function download($table, $field, $path_save)
	{
		$sets = $this->db
			->get($table)->result();
		foreach ($sets as $item)  {
			$name = basename($item->$field);
			if ($name) {
				file_put_contents($path_save . $name , file_get_contents($item->$field));
			}
		}
		
	}
}